<?php

namespace app\admin\controller;

use think\Controller;

use think\Db;
use think\Request;
use think\File;
class Photo extends Controller
{

    public function index()
    {
        $this->fetch('photo/photo');
    }


    public function edit()
    {


        $id = input('id');
        $photo = db('to_photo')->find($id);

        $img = $photo['img'];
        $title = $photo['title'];

        return $this->fetch('photo/edit', [
            'id' => $id,
            'img' => $img,
            'title' => $title

        ]);


    }

    public function edits(){
        $id = input('id');
        // dump($id);die;
        $links = db('to_photo')->find($id);

        if (request()->isPost()) {
            $data = [
                'id' => $id,
                'img' => input('img'),
                'title' => input('title'),
            ];


//            $validate = \think\Loader::validate('Links');
//            if (!$validate->check($data)) {
//                $this->error($validate->getError());
//                die;
//            }
            if (db('to_photo')->update($data)) {
                $this->success('修改图片成功',url('admin/photo/index'));

            } else {
                $this->error('修改图片失败');
            }
            return;
        }
    }

    public function add()
    {
        return view('photo/add');
    }

//查询广告
    public function photo_select()
    {

        $list = Db::name('to_photo')->field(['id',  'img','title'])->select();
        return $this->fetch('photo/photo', [
            'list' => $list,
        ]);
    }


//    编辑广告
    public function photo_edit(Request $request)
    {
        $data = Request::instance()->post();

        $id=$data['id'];
        $file = request()->file('img');

        if($file) {
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->validate(['size' => 1500000, 'ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');

//          var_dump($info) ;die;
        }
        if(!empty($info)){
            // 成功上传后 获取上传信息
            $a=$info->getSaveName();
            $imgp= str_replace("\\","/",$a);
            $data['img']= $imgp;
            $resd=Db::table('to_photo')->where('id',$id)->update($data);
            if($resd){
                return $this->success('更新图片成功',url('admin/photo/photo_select'));
            }else{
                return $this->success('更新图片失败');
            }
        }else{
            return $this->error('图片不能为空');
        }
    }
// 添加图片
    public function photo_add(Request $request)
    {
        $file = request()->file('img');
        $data = Request::instance()->post();

        if ($file) {
            $info = $file->validate(['size' => 156780000, 'ext' => 'jpg,jpeg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
        }


        if (!empty($info)) {
            // 成功上传后 获取上传信息
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            $img = $info->getSaveName();
            $imgs = str_replace('\\', '/', $img);

            $result = Db::table('to_photo')
                ->data(['title' => $data['title'],'img'=>$imgs]) ->insert();
            if ($result) {
                return $this->success('添加图片成功', url('admin/photo/photo_select'));
            } else {
                return $this->error('添加图片失败');
            }
        }
        else {
            return $this->error('图片不能为空');
        }
    }
    // 删除小广告
    public function del()
    {
        $id =input('id');
        if (db('to_photo')->delete($id)) {
            $this->success('删除照片成功', 'admin/photo/photo_select');
        } else {
            $this->error('删除照片失败');
        }
        return;

        return $this->redirect('admin/photo/photo_select');
    }


}
